<template>
	<view class="load" v-if="!load">
		<u-loading-icon mode="circle" inactive-color="#fff" text="正在加载..." vertical></u-loading-icon>
	</view>
	<block v-else>
		<view class="wrap">
			<view class="shop_body">
				<view class="shop_cell">
					<view class="cell_logo">
						<image :src="product.img"
							mode="aspectFill"></image>
					</view>
					<view class="cell_main">
						<view class="main_top">
							<view class="tit hide">
								{{ product.ProductName }}
							</view>
							<view class="tag_body" style="display: none;">
								<view class="tag_cell" v-for="(el,i) in tag" :key="i">
									{{el}}
								</view>
							</view>
							<view class="desc">
								{{ product.ProductStyle }}
							</view>
						</view>

						<view class="main_foot">
							<view class="foot_price">
								<text>￥</text>{{ product.Price }}
							</view>
							<view class="foot_count">
								库存：{{ product.StockNum }}
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="shop_sendrInfo">
				<view class="shop_person">
					<view class="tit">
						赠送主题
					</view>
					<input type="text" v-model="senderTitle" placeholder="请填写赠送主题" />
				</view>
				
				<view class="shop_person">
					<view class="tit">
						快递发件人
					</view>
					<input type="text" v-model="senderName" placeholder="请填写快递发件人名称" />
				</view>
				
				<view class="shop_person">
					<view class="tit">
						快递发件人号码
					</view>
					<input type="text" v-model="senderPhone" placeholder="请填写快递发件人号码" />
				</view>
			</view>
			<view class="add_person">
				<view class="person_cell" @click="checkCustom()">
					<view class="icon">
						<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/172222145037569.png"
							mode="aspectFill">
						</image>
					</view>
					<view class="tit">
						选择赠送客户
					</view>
				</view>
				<view class="person_cell" @click="nav_to('/pages/custom/import?type=2')">
					<view class="tit" style="color: #333;">
						批量导入
					</view>
				</view>
			</view>
			<view class="custom_body"  v-if="selectedCustomers.length > 0">
				<view class="custom_cell" v-for="(item, index) in selectedCustomers" :key="index">
					<view class="custom_info">
						<view class="info_body">
							<view class="name">
								{{ item.TrueName }}
							</view>
							<view class="tag" style="display: none;">
								{{ item.Company }}
							</view>
						</view>
						<view class="del" @click="removeCustomer(index)">
							移除
						</view>
					</view>
					<view class="custom_desc">
						<view class="icon">
							<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/17222231328751473.png"
								mode="aspectFill"></image>
						</view>
						<view class="tit">
							{{ item.Phone }}
						</view>
					</view>
					<view class="custom_desc last_desc">
						<view class="icon">
							<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/17222231730401127.png"
								mode="aspectFill"></image>
						</view>
						<view class="tit">
							{{ item.Province }}{{ item.City }}
							<view v-if="item.Area">{{ item.Area }}</view>
							{{ item.Address }}
						</view>
					</view>

					<view class="send_body">
						<view class="tit">
							赠送数量：
						</view>
						<view class="send_cell">
							<view class="icon" @click="countFun(item,0)">
								<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/1722223364639933.png"
									mode="aspectFill"></image>
							</view>
							<input type="number" v-model="item.qty" />
							<view class="icon" @click="countFun(item,1)">
								<image src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/1722223336975457.png"
									mode="aspectFill"></image>
							</view>
						</view>
					</view>
				</view>
			</view>

			<view style="height: 220rpx;"></view>
			<view class="btn_body">
				<view class="tips_body" v-if="isImport==1"  @click="changeIsSaveBook()">
					<radio class="radio" :checked="IsSaveBook" color="#ff6030"></radio>
					<view class="tips_cell">
						是否需要将导入的客户信息保存至我的客户
					</view>
				</view>
				<view class="btn_cell" @click="confirmGive">
					确认赠送
				</view>
			</view>
		</view>
	</block>
</template>

<script>
	export default {
		data() {
			return {
				load: false,
				tag: ['正宗', '正宗产', '正宗产地'],
				count: 1,
				product: {},
				senderTitle: '',
				senderName: '',
				senderPhone: '',
				key: '',
				selectedCustomers: [],
				
				IsSaveBook:0,
				isImport:0
			};
		},
		computed: {
			
		},

		watch: {

		},

		onReady() {

		},
		async onLoad(e) {
			this.product = this.$store.state.sendProduct;
			// this.key = e.ABC
			// this.getDetails(this.key);
			this.load = true;
		},
		async onShow() {
			uni.$on('select', data => {
				this.selectedCustomers = data.map(item => ({
					...item,
					qty: 1
				}))
			})
			uni.$on('customUpdate',data=>{
				console.log('custom===>>>>>>>>>',data);
				if(data){
					this.isImport = 1
					this.selectedCustomers = data.map(item => ({
						...item,
						qty: 1
					}))					
				}
			})
		},
		
		onUnload() {
			uni.$off('select');
			uni.$off('customUpdate');
		},

		methods: {
			changeIsSaveBook(){
				this.IsSaveBook = this.IsSaveBook==1 ? 0 : 1
			},
			checkCustom(){
				let arr = []
				if(this.selectedCustomers.length){
					this.selectedCustomers.forEach(item=>{
						arr.push(item.ID)
					})
					this.$store.commit('setcustomIDList',arr)
				}
				this.nav_to(`/pages/custom/selectCustomList`)
			},
			countFun(item,i) {
				if (i == 0 && item.qty > 1) {
					item.qty--
				}
				if (i == 1) {
					item.qty++
				}
			},
			
			async getDetails(key) {
				let res = await this.$api.post('/SendGift/Detail', {
					BatchNo: key,
				});
				
				if (res.code == 0) {
					this.product = res.data
					this.product.img = this.product.Images.split(',')[0];
				}
			},
			
			// 确认赠送（新建批次-转赠客户）
			async confirmGive() {
				let TotalQty = this.selectedCustomers.reduce((total, item) => total + item.qty, 0);
				
				if(this.product.StockNum<this.selectedCustomers.length){
					this.$pv.showModel('库存不足，马上去购买~',()=>{
						this.nav_to(`/pages/shop/detail?id=${this.product.ProductID}`)
					})
					return
				}
				
				if (!this.senderTitle || !this.senderName || !this.senderPhone) {
					this.$pv.msg('请填写完整的发件信息')
					return;
				}
				console.log(this.selectedCustomers.length);
				if(!this.selectedCustomers.length){
					this.$pv.msg('请选择赠送客户')
					return
				}
				
				let response = await this.$api.post('/SendGift/Add', {
					ProductID: this.product.ProductID,
					ProductStyle: this.product.ProductStyle,
					Qty: TotalQty,
					Title: this.senderTitle,
					SenderName: this.senderName,
					SenderMobile: this.senderPhone,
					IsSingleSend:0,
					IsSaveBook:this.IsSaveBook
				});
				
				if (response.code === 0) {
					this.onlyKey = response.data
					
					// 封装产品明细和客户明细
					let obj = {
						ProductID: this.product.ProductID,
						ProductStyle: this.product.ProductStyle,
						Qty: TotalQty,
					}
					
					// 构建客户明细数组
					let arr = this.selectedCustomers.map((item) => ({
					    ID: item.ID,
					    Qty: item.qty
					}))
					
					let res = await this.$api.post('/SendGift/Donation', {
						ProductItemStr: JSON.stringify([obj]),
						CustomerStr: JSON.stringify(arr),
						SendBatchNo: this.onlyKey,
						SenderName:this.senderName,
						SenderPhone:this.senderPhone
					});
					
					if (res.code == 0) {
						// 跳转赠送成功页面
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/gift/giveSuccess?id=${this.product.ProductID}`
							});
						}, 1000);
					}
				}
			},
		
			// 移除客户
			removeCustomer(index) {
			    this.selectedCustomers.splice(index, 1);
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f6f6f6;
	}

	image {
		width: 100%;
		height: 100%;
	}

	.hide {
		overflow: hidden;
		/*超出部分隐藏*/
		white-space: nowrap;
		/*禁止换行*/
		text-overflow: ellipsis;
		/*省略号*/
	}

	.wrap {
		.shop_body {
			background-color: #fff;
			margin: 16rpx 0;

			.shop_cell {
				padding: 24rpx 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				position: relative;

				&::after {
					content: '';
					width: 90%;
					height: 2rpx;
					background-color: #eee;
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translate(-50%, 0);
				}

				.cell_logo {
					width: 204rpx;
					height: 204rpx;
					overflow: hidden;
					border-radius: 8rpx;
					margin-right: 24rpx;
				}

				.cell_main {
					flex: 1;
					display: flex;
					justify-content: space-between;
					flex-direction: column;
					height: 190rpx;

					.main_top {
						.tit {
							width: calc(750rpx - 48rpx - 202rpx - 24rpx);
							font-weight: bold;
							font-size: 28rpx;
							color: #333333;
							margin-bottom: 8rpx;
						}

						.tag_body {
							display: flex;
							align-items: center;
							margin-bottom: 8rpx;

							.tag_cell {
								font-size: 20rpx;
								color: #F91C1C;
								padding: 4rpx 12rpx;
								border-radius: 8rpx;
								border: 1rpx solid #F91C1C;
								margin-right: 8rpx;
							}

							.tag_cell:last-child {
								margin-right: 0;
							}
						}

						.desc {
							font-size: 24rpx;
							color: #999999;
						}
					}

					.main_foot {
						margin-top: 26rpx;
						display: flex;
						justify-content: space-between;

						.foot_price {
							font-weight: bold;
							font-size: 36rpx;
							color: #F91C1C;

							text {
								font-weight: bold;
								font-size: 24rpx;
								color: #F91C1C;
							}
						}

						.foot_count {
							font-size: 24rpx;
							color: #999999;
							display: flex;
							align-items: center;
						}
					}
				}
			}

			.shop_cell:last-child::after {
				width: 0;
			}
		}
		
		.shop_sendrInfo {
			margin-bottom: 16rpx;
			background-color: #fff;
			padding: 0 0 0 32rpx;
			
			.shop_person {
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1rpx solid #E4E4E4;
				padding: 0 32rpx 0 0;
			
				.tit {
					font-size: 32rpx;
					color: #333333;
				}
			
				input {
					font-size: 32rpx;
					padding: 32rpx 0;
					text-align: right;
				}
			
				input::placeholder {
					text-align: right;
				}
			}
			.shop_person:last-child {
				border-bottom: none;
			}
		}

		
		.add_person {
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;
			
			padding: 32rpx;
			.person_cell{
				display: flex;
				align-items: center;
				justify-content: normal;
				.tit {
					color: #FF6030;
					margin-left: 16rpx;
				}
				
				.icon {
					width: 40rpx;
					height: 40rpx;
				}
			}
			
		}

		.custom_body {
			background-color: #fff;
			padding: 0 32rpx 32rpx;

			.custom_cell {
				padding: 24rpx;
				box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.08);
				border-radius: 16rpx;
				margin-bottom: 20rpx;

				.custom_info {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 6rpx;
					.info_body {
						display: flex;
						align-items: center;

						.name {
							font-weight: bold;
							font-size: 32rpx;
							color: #333333;
						}

						.tag {
							font-size: 20rpx;
							color: #FF6030;
							background: #FFF3F0;
							border-radius: 4rpx;
							padding: 2rpx 8rpx;
						}
					}

					.del {
						font-size: 24rpx;
						color: #333333;
						padding: 8rpx 24rpx;
						background: #F6F6F6;
						border-radius: 24rpx;
					}
				}

				.custom_desc {
					display: flex;
					align-items: center;
					margin-bottom: 12rpx;

					.icon {
						width: 32rpx;
						height: 32rpx;
						margin-right: 8rpx;
					}

					.tit {
						font-size: 28rpx;
						color: #666666;
						display: flex;
					}
				}

				.last_desc {
					padding-bottom: 24rpx;
					border-bottom: 2rpx solid #E4E4E4;
					margin-bottom: 0;
				}

				.send_body {
					display: flex;
					align-items: center;
					justify-content: flex-end;
					padding-top: 24rpx;

					.tit {
						font-size: 28rpx;
						color: #333333;
					}

					.send_cell {
						display: flex;
						align-items: center;

						.icon {
							padding: 10rpx;
							display: flex;
							align-items: center;
							justify-content: center;

							image {
								width: 32rpx;
								height: 32rpx;
							}
						}

						input {
							width: 80rpx;
							margin: 0 4rpx;
							text-align: center;
							background: #F6F6F6;
							border-radius: 8rpx;
							padding: 2rpx 8rpx;
						}
					}
				}
			}

			.custom_cell:last-child {
				margin-bottom: 0;
			}
		}



		.btn_body {
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 99;
			width: 100%;
			padding: 20rpx 40rpx 60rpx;
			box-sizing: border-box;
			background-color: #fff;

			.btn_cell {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 18rpx 0;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
				background: linear-gradient(98deg, #FFAD26 0%, #FE4E01 100%);
				border-radius: 44rpx;
			}
			.tips_body{
				display: flex;
				align-items: center;
				margin-bottom: 10rpx;
				.radio{
					transform:scale(0.65)
				}
				.tips_cell{
					font-size: 26rpx;
					color: #999999;
				}
			}
		}
	}
</style>